// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   Utils.java

package app;

import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Image;

public class Utils
{

    static String[] getArrayFitString(Font font, String label, int width) {
        return getArrayFitString(font, label, width, false, false);
    }

    public Utils()
    {
        java.io.InputStream inputstream = null;
        inputstream = getClass().getResourceAsStream("data/conf.txt");
        
        try {
            StringBuffer sb = new StringBuffer();
            int chr, i = 0;
            while((chr = inputstream.read())!=-1) {
                sb.append((char) chr);
            }
            _pagestart = Integer.parseInt(sb.toString());
        } catch(Exception e) {
            
        }
    }

    public static int getPageStart()
    {
        return _pagestart;
    }
    
    public static Image getImage(int t)
    {
        if(images[t] != null)
            return images[t];
        String img = null;
        switch(t)
        {
        case 0: // '\0'
            img = "/images/header.png";
            break;
        }
        try
        {
            images[t] = Image.createImage(img);
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
        return images[t];
    }

    public static String[] getArrayFitString(Font font, String s, int width, boolean flagz, boolean header)
    {
        if(font == null || s == null || width <= 0 || s.length() == 0)
            return new String[0];
        int cumwidth[] = cumulativeWidth(font, s);
        if(cumwidth[0] <= width)
        {
            if(!flagz && !header && s.charAt(0) == '\n')
                s = s.substring(1, s.length());
            return (new String[] {
                s
            });
        }
        Vector vector = new Vector();
        int k = s.length();
        int l = s.indexOf(' ');
        if(l >= 0)
        {
            int i1 = 0;
            int l2;
            for(int k1 = 1; k1 < cumwidth.length; k1 = (i1 = l2) + 1)
            {
                int i2 = 0;
                boolean flag1 = false;
                do
                {
                    if(k1 >= cumwidth.length || i2 + cumwidth[k1] > width)
                        break;
                    if(s.charAt(k1 - 1) == '\n')
                    {
                        flag1 = true;
                        k1++;
                        break;
                    }
                    i2 += cumwidth[k1++];
                } while(true);
                l2 = k1 - 1;
                String s2 = s.substring(i1, flag1 && l2 > i1 ? l2 - 1 : l2);
                for(; l2 < k && s.charAt(l2) == ' '; l2++);
                int i3;
                if(!flag1 && l2 == k1 - 1 && k1 < cumwidth.length && (i3 = s2.lastIndexOf(' ')) >= 0)
                {
                    l2 = i1 + i3;
                    s2 = s.substring(i1, l2);
                    l2++;
                }
                vector.addElement(s2);
            }

        }
        String as[] = new String[vector.size()];
        vector.copyInto(as);
        return as;
    }

    private static int[] cumulativeWidth(Font font, String s)
    {
        int i = s.length();
        int ai[] = new int[i + 1];
        int total = 0;
        for(int k = 1; k <= i; k++)
        {
            char c1 = s.charAt(k - 1);
            total += ai[k] = font.charWidth(c1);
        }

        ai[0] = total;
        return ai;
    }

    public static int parseHex(String string)
    {
        int r = 0;
        for(int i = 0; i < string.length(); i++)
        {
            char ch = Character.toLowerCase(string.charAt(i));
            if(ch >= '0' && ch <= '9')
            {
                r <<= 4;
                r += ch - 48;
                continue;
            }
            if(ch >= 'a' && ch <= 'f')
            {
                r <<= 4;
                r += ch - 97;
            }
        }

        return r;
    }

    public static final int IMAGE_HEADER = 0;
    private static int _pagestart = 0;
    private static Image images[] = new Image[1];

}
